import tkinter as tk
from tkinter import messagebox
import sys
import os

# Add project root to Python path
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(os.path.dirname(os.path.dirname(current_dir)))
sys.path.insert(0, project_root)

from src.storage.file_manager import save_to_file, load_from_file

def create_main_window():
    """Create and return main window"""
    # Load data
    items = load_from_file()
    
    # Create main window
    root = tk.Tk()
    root.title("Game Character Collection Manager")
    root.geometry("600x500")
    root.resizable(True, True)
    
    # Set up close event handler
    def on_closing():
        if messagebox.askokcancel("Quit", "Are you sure you want to quit? Data will be saved automatically."):
            save_to_file(items, "data/items.json")
            root.destroy()
    
    root.protocol("WM_DELETE_WINDOW", on_closing)
    
    # Lazy import to avoid circular dependencies
    from .screens.launch_screen import show_launch_screen
    show_launch_screen(root, items)
    
    return root, items

def run_gui():
    """Run GUI application"""
    root, items = create_main_window()
    root.mainloop()